import os

import re
path='C:\\Users\\liuqimin\\Downloads\\理论力学第八版\\tmp_file'
out_path=os.path.join(path, 'all')
if not os.path.exists(out_path):
    os.makedirs(out_path)

#-------------------first step, rename all image files
for dir0 in os.listdir(path):

    for f1 in os.listdir(os.path.join(path, dir0)):
        src=os.path.join(path, dir0, f1)
        dst=os.path.join(path, dir0, f1.zfill(7))
        os.rename(src , dst)

# ####---------
#
# -----old version--
# x=[ '一', '二',  '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五', '十六' ]
# tt=dict()
# for i  in range(0,16):
#     tt[x[i]]=i+1
#
# ###-----------raname directory---------
# for dir0 in os.listdir(path):
#     src=os.path.join(path, dir0)
#
#     if dir0=='all':
#         continue
#     chapter = re.findall(r'第(.{1,2})章', dir0)
#
#     num = tt[chapter[0]]
#
#     if '知识点'  in dir0:
#         dst=os.path.join(path, str(num).zfill(2)+'_1')
#         os.rename(src, dst)
#
#     else:
#         dst = os.path.join(path, str(num).zfill(2) + '_2')
#         os.rename(src, dst)
#
#
#
# #-------------step 3------------------
# for item in os.listdir(path):
#
#     chapter=re.findall(r'第(.{1,2})章', item)
#     if item=='all':
#         continue
#     print('chapter=', chapter, 'item=', item)
#     num=tt[chapter[0]]
#     for f2 in os.listdir(os.path.join(path, item)):
#         zz=f2.split('.')
#         src='"'+os.path.join(path, item, f2)+'"'
#         dst='"'+ os.path.join(path, 'all', str(num).zfill(2)+'.'+f2.zfill(7)) + '"'
#         print('copy ' + src + ' ' + dst)
#         os.system('copy ' + src + ' ' + dst )

